/*
 * Copyright © 2018 CODESTD.COM Inc. All rights reserved.
 */
package com.codestd.security.shiro.jwt;

import com.codestd.security.model.SysUser;
import com.codestd.security.redis.model.AndroidUserRefreshToken;
import com.codestd.security.redis.model.IosUserRefreshToken;
import com.codestd.security.redis.model.RefreshToken;
import com.codestd.security.redis.repository.AndroidUserRefreshTokenRedisRepository;
import com.codestd.security.redis.repository.IosUserRefreshTokenRedisRepository;
import com.codestd.security.redis.repository.RefreshTokenRedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.UUID;

/**
 * 移动端Token处理
 *
 * @author jaune
 * @since 1.0.0
 */
@Component
public class MobileTokenManagerImpl implements MobileTokenManager {

    @Autowired
    private RefreshTokenRedisRepository refreshTokenRedisRepository;

    @Autowired
    private AndroidUserRefreshTokenRedisRepository androidUserRefreshTokenRedisRepository;

    @Autowired
    private IosUserRefreshTokenRedisRepository iosUserRefreshTokenRedisRepository;

    @Override
    public String createRefreshToken(SysUser user, String type) {
        String refreshTokenStr = uuid();
        RefreshToken refreshToken = new RefreshToken(refreshTokenStr, user.getUserId(), type, 30L);
        if ("1".equals(type)) {
            AndroidUserRefreshToken androidUserRefreshToken =
                    new AndroidUserRefreshToken(user.getUserId(), refreshTokenStr, 30L);
            this.androidUserRefreshTokenRedisRepository.save(androidUserRefreshToken);
        } else {
            IosUserRefreshToken iosUserRefreshToken =
                    new IosUserRefreshToken(user.getUserId(), refreshTokenStr, 30L);
            this.iosUserRefreshTokenRedisRepository.save(iosUserRefreshToken);
        }
        this.refreshTokenRedisRepository.save(refreshToken);

        return refreshTokenStr;
    }

    @Override
    public void deleteRefreshToken(String userId, String type) {
        if ("1".equals(type)) {
            Optional<AndroidUserRefreshToken> optional = this.androidUserRefreshTokenRedisRepository.findById(userId);
            if (optional.isPresent()) {
                this.androidUserRefreshTokenRedisRepository.deleteById(userId);
                this.refreshTokenRedisRepository.deleteById(optional.get().getRefreshToken());
            }
        } else {
            Optional<IosUserRefreshToken> optional = this.iosUserRefreshTokenRedisRepository.findById(userId);
            if (optional.isPresent()) {
                this.iosUserRefreshTokenRedisRepository.deleteById(userId);
                this.refreshTokenRedisRepository.deleteById(optional.get().getRefreshToken());
            }
        }
    }

    @Override
    public Optional<RefreshToken> getRefreshToken(String refreshToken) {
        return this.refreshTokenRedisRepository.findById(refreshToken);
    }

    private String uuid() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "");
    }
}
